﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace PLL
{
    public class LogManager
    {
        private static string logPath = @"AuthorWrite\PayLog\";               // 日志文件夹
        private static string logFolder = "log";               // 文件夹名
        private static string lonExtension = ".Log";           // 日志文件类型

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogFile logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFolder,LogFile logFile, string msg)
        {
            WriteLog(logFolder,logFile.ToString(), msg);
        }
        
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, string msg)
        {
            try
            {
                DirectoryExists();
                DeleteLog(logFile, "");
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    " + msg);
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFolder,string logFile, string msg)
        {
            try
            {
                DirectoryExists(logFolder);
                DeleteLog(logFile, "");
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    " + msg);
                sw.Close();
            }
            catch
            { }
        }


        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logFile">日志文件类型</param>
        /// <param name="logFileName">日志文件名</param>
        /// <param name="msg">信息说明</param>
        public static void WriteLog( ArrayList al,string uid)
        {
            try
            {
                DirectoryExists();
                string logPathName = AppDomain.CurrentDomain.BaseDirectory + logPath + uid + "_" + "ShopCartLog.txt";
                if(!File.Exists(logPathName)){
                    FileStream NewText = File.Create(logPathName);
                    NewText.Close();
                }
                System.IO.FileStream fs = new System.IO.FileStream(logPathName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
    StreamWriter swWriter = File.AppendText(logPathName);
                for (int i = 0; i < al.Count; i++)
                {
                    swWriter.WriteLine(al[i].ToString());
                }
                swWriter.Flush();
                swWriter.Close();

            }
            catch
            { }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logFolder">日志文件夹</param>
        /// <param name="logFile">日志文件类型</param>
        /// <param name="logFileName">日志文件名</param>
        /// <param name="msg">信息说明</param>
        public static void WriteLog(string logFolder,string logFile, string logFileName, ArrayList al)
        {
            try
            {
                DirectoryExists(logFolder);
                DeleteLog(logFile, logFileName);
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + "_" + logFileName + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                for (int i = 0; i < al.Count; i++)
                {
                    sw.WriteLine(al[i].ToString());
                }
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        public static void DirectoryExists()
        {
            if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + logFolder) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + logFolder);
            }
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="logFileName"></param>
        public static void DirectoryExists(string logFolder)
        {
            if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + logFolder) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + logFolder);
            }
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="logFile"></param>
        /// <param name="logFileName"></param>
        public static void DeleteLog(string logFile, string logFileName)
        {
            string logPathName = AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + "_" + logFileName ;
            FileInfo file = new FileInfo(logPathName);
            if (!file.Exists)
            {
                 file.Create();
          }

        }
        
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Trace,
        Warning,
        Error,
        SQL
    }
}



